const htmlwebpackPlugin = require('html-webpack-plugin')

module.exports = {
    entry: {
        base: "./src/base.js",
        login: "./src/pages/login/index.js",
        userAdd: "./src/pages/userAdd/index.js",
        users: "./src/pages/users/index.js",
        userUpdate: "./src/pages/userUpdate/index.js",
    },
    output: {
        path: __dirname + '/dist',
        filename: "js/[name][hash:8].js",
        clean: true
    },
    module: {
        rules: [
            // 处理JS文件 ES6 => ES5
            {
                test: /\.js$/,
                exclude: /node_modules/, // 不进行处理的文件夹
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            [
                                '@babel/preset-env',
                                {
                                    useBuiltIns: 'usage', // 只打包使用的ES6新API的实现代码
                                    corejs: { version: 2 } // 指定core-js的版本号为2
                                }
                            ]
                        ]
                    }
                }
            },
            // 处理css文件
            {
                test: /\.css$/,  // 配置css文件的正则
                use: [
                    'style-loader', // js文件 => 页面的style标签
                    'css-loader'  // css文件 => js文件
                ]
            },
            // 处理art文件
            {
                test: /\.art$/,
                loader: "art-template-loader",
            },
        ]
    },
    mode: "development",
    plugins: [
        new htmlwebpackPlugin({
            template: "./src/pages/index/index.html",
            filename: 'index.html',
            chunks: ['base'],
            inject: "body"
        }),
        new htmlwebpackPlugin({
            template: "./src/pages/login/index.html",
            filename: 'login.html',
            chunks: ['base', 'login'],
            inject: "body"
        }),
        new htmlwebpackPlugin({
            template: "./src/pages/userAdd/index.html",
            filename: 'userAdd.html',
            chunks: ['base', 'userAdd'],
            inject: "body"
        }),
        new htmlwebpackPlugin({
            template: "./src/pages/users/index.html",
            filename: 'users.html',
            chunks: ['base', 'users'],
            inject: "body"
        }),
        new htmlwebpackPlugin({
            template: "./src/pages/userUpdate/index.html",
            filename: 'userUpdate.html',
            chunks: ['base', 'userUpdate'],
            inject: "body"
        })
    ],
    devServer: {
        port: 3000,
        open: true,
        proxy:{
            "/api":{
                target:"http://localhost:8081",
                pathRewrite:{
                    "^/api":""
                }
            }
        }
    },
     //增加sourceMap
    devtool: 'cheap-module-source-map',
    
}